//Copyright 2009 Dominik Punz
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

#ifndef Desktop_h__
#define Desktop_h__

#include "resource.h"       // main symbols
#include "SilkTestLib_i.h"
#include "jtfDesktop.h"
#include "objectFactory.h"
#include <map>

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

// CDesktop

class ATL_NO_VTABLE CDesktop :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CDesktop, &CLSID_Desktop>,
  public ISupportErrorInfo,
	public IDispatchImpl<IDesktop, &IID_IDesktop, &LIBID_SilkTestLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
  public CObjectFactory
{
public:
	CDesktop() : mpJtfDesktop(0) {}

DECLARE_REGISTRY_RESOURCEID(IDR_DESKTOP)

BEGIN_COM_MAP(CDesktop)
	COM_INTERFACE_ENTRY(IDesktop)
	COM_INTERFACE_ENTRY(IDispatch)
  COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()

// ISupportsErrorInfo
  STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
    delete mpJtfDesktop;
	}

public:
  STDMETHOD(Connect)(BSTR hostname, LONG port);
  STDMETHOD(Attach)(BSTR moduleNamePattern, BSTR commandLinePattern, BSTR techDomains);
  STDMETHOD(DetachAll)(void);
  STDMETHOD(ExecuteBaseState)(BSTR executable, BSTR commandLine, BSTR mainWindowLocator, BSTR techDomains, IDispatch **mainWindow);
  STDMETHOD(ExecuteBrowserBaseState)(BSTR executable, BSTR url, BSTR mainWindowLocator, BSTR techDomains, IDispatch **mainWindow);
  STDMETHOD(Find)(BSTR locator, IDispatch** testObject);
  STDMETHOD(FindAll)(BSTR locator, IDispatch** testObjects);
  STDMETHOD(GetParent)(IDispatch** testObject);
  STDMETHOD(GetChildren)(IDispatch** testObjects);
  STDMETHOD(GetOption)(BSTR optionName, VARIANT* optionValue);
  STDMETHOD(GetTechDomainOption)(BSTR optionName, BSTR techDomainName, VARIANT* optionValue);
  STDMETHOD(ResetAllOptions)(void);
  STDMETHOD(SetOption)(BSTR optionName, VARIANT optionValue);
  STDMETHOD(SetTechDomainOption)(BSTR optionName, BSTR techDomainName, VARIANT optionValue);

public:
  virtual IDispatch* CreateDispTestObject(jobject jobjTestObject);
  virtual IDispatch* CreateDispTestObjectList(jobject jobjListOfTestObjects);
  void UnregisterTestObject(jobject jobjTestObject);

private:
  CJtfDesktop* GetJtfDesktop();
  int GetHashCode(jobject jobjTestObject);

private:
	CJtfDesktop* mpJtfDesktop;
  std::map<int, IDispatch*> mDispTestObjects;
};

OBJECT_ENTRY_AUTO(__uuidof(Desktop), CDesktop)


#endif // Desktop_h__
